package cbfg.popup.demo

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import cbfg.popup.PopupWindowBuilder
import kotlinx.android.synthetic.main.fragment_popup_window.*
import kotlinx.android.synthetic.main.fragment_slide_in_bottom.view.*
import kotlinx.android.synthetic.main.popup_options.view.*

/**
 * @author:  Tom Hawk
 * 添加时间: 2020/9/7 16:38
 * 功能描述:
 */
class PopupWindowFragment : Fragment(R.layout.fragment_popup_window), View.OnClickListener {
    private val options = arrayListOf("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh")

    private val optionsPopup: PopupWindow by lazy {
        PopupWindowBuilder(requireContext(), R.layout.popup_options)
            .setLayoutListener(object : PopupWindowBuilder.LayoutListener {
                override fun onViewCreated(view: View) {
                    val lvOptions = view.lvOptions
                    lvOptions.adapter = ArrayAdapter<String>(
                        view.context,
                        android.R.layout.simple_list_item_1,
                        options
                    )
                    lvOptions.setOnItemClickListener { _, _, i, _ ->
                        tvSelection.text = options[i]
                        optionsPopup.dismiss()
                    }
                }
            })
            .setDismissListener(PopupWindow.OnDismissListener {
                tvSelection.isSelected = false
            })
            .create()
    }

    private val slideInBottomPopup: PopupWindow by lazy {
        PopupWindowBuilder(requireContext(), R.layout.fragment_slide_in_bottom)
            .setAnimStyle(R.style.SlideInFromBottomAnim)
            .setLayoutListener(object : PopupWindowBuilder.LayoutListener {
                override fun onViewCreated(view: View) {
                    val listener = View.OnClickListener {
                        Toast.makeText(
                            it.context,
                            "Click ${(it as TextView).text}",
                            Toast.LENGTH_SHORT
                        ).show()
                        slideInBottomPopup.dismiss()
                    }
                    view.tvOption1.setOnClickListener(listener)
                    view.tvOption2.setOnClickListener(listener)
                    view.tvOption3.setOnClickListener(listener)
                }
            })
            .create()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btnTopStart.setOnClickListener(this)
        btnTopEnd.setOnClickListener(this)
        btnBottomStart.setOnClickListener(this)
        btnBottomEnd.setOnClickListener(this)
        btnCenter.setOnClickListener(this)
        btnAtBottom.setOnClickListener(this)
        tvSelection.setOnClickListener(this)
        btnSlideInBottom.setOnClickListener(this)
        btnInput.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btnTopStart -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_top_start)
                    .resize(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                    .setDimAmount(0.1F)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }

            R.id.btnTopEnd -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_top_end)
                    .resize(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                    .setDimAmount(0.2F)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }

            R.id.btnBottomStart -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_bottom_start)
                    .resize(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                    .setDimAmount(0.3F)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }

            R.id.btnBottomEnd -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_bottom_end)
                    .resize(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                    .setDimAmount(0.4F)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }

            R.id.btnCenter -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_center)
                    .resize(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                    .setDimAmount(0.5F)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }

            R.id.btnAtBottom -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_center)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }

            R.id.tvSelection -> {
                tvSelection.isSelected = true
                optionsPopup.showAsDropDown(v)
            }

            R.id.btnSlideInBottom -> {
                slideInBottomPopup.showAsDropDown(v)
            }
            R.id.btnInput -> {
                PopupWindowBuilder(v.context, R.layout.fragment_df_input)
                    .setDismissListener(PopupWindow.OnDismissListener {
                        Toast.makeText(v.context, "Dismiss", Toast.LENGTH_SHORT).show()
                    })
                    .create()
                    .showAsDropDown(v)
            }
        }
    }
}